package com.example.demo.wrap;

import com.example.demo.config.constant.DBCacheKey;
import com.example.demo.dao.dataSource1.GeographyMapper;
import com.example.demo.dao.dataSource1.StoryContentMapper;
import com.example.demo.dao.dataSource1.StoryMapper;
import com.example.demo.entity.bean.dataSource1.StoryBean;
import com.example.demo.entity.dataSource1.GeographyEntity;
import com.example.demo.entity.dataSource1.StoryContentEntity;
import com.example.demo.entity.dataSource1.StoryEntity;
import com.example.demo.entity.dataSource1.StoryToGeographyEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * wrapper对于story及周边的技术性CRUD操作,并封装缓存
 * Created by Wen Gao on 2018/7/25.
 */
@Repository
@Transactional(transactionManager = "xatx", propagation = Propagation.REQUIRED, rollbackFor = Throwable.class)
public class StoryWrapper {

    @Autowired
    private StoryMapper storyMapper;
    @Autowired
    private StoryContentMapper storyContentMapper;
    @Autowired
    private GeographyMapper geographyMapper;

    /************************************************************************************************************************************************************************
     *  Story全量方法,不直接参与缓存
     ************************************************************************************************************************************************************************/
    /**
     * <li>1</li>
     * <li>2</li>
     * <li>3</li>
     * @param storyBean 要insert的全量storyBean
     * */
    public void insertFullStoryBean(StoryBean storyBean) {
        // todo 数据校验
        // 1. 插入story表并获取自增storyId
        int savedStoryId = insertStoryEntity(storyBean.getStoryEntity()).getId();
        // 2. 将自增storyId赋予content数据并插入content表
         storyBean.getStoryContentEntity().setId(savedStoryId);
        insertStoryContentEntity(storyBean.getStoryContentEntity());
        // 3. 处理s2g
        for(StoryToGeographyEntity s2gEntity : storyBean.getStoryToGeographyEntityList()) {
            s2gEntity.setStoryId(savedStoryId);
            insertStoryToGeography(s2gEntity);
        }

    }

    public void updateFullStoryBean(StoryBean storyBean) {
        // todo
    }

    public StoryBean selectFullStoryBean(int storyId) {
        // todo
        return null;
    }

    /************************************************************************************************************************************************************************
     *  Story相关方法
     ************************************************************************************************************************************************************************/
    @CachePut(value= DBCacheKey.STORY,key="#result.id")
    public StoryEntity insertStoryEntity(StoryEntity storyEntity) {
        storyMapper.insert(storyEntity);
        return storyEntity;
    }

    @CacheEvict(value= DBCacheKey.STORY,key="#p0.id")
    public void updateStoryEntity(StoryEntity storyEntity) {
        storyMapper.update(storyEntity);
    }

    @Cacheable(value= DBCacheKey.STORY,key="#p0")
    public StoryEntity selectStoryEntity(int storyId) {
        return storyMapper.select(storyId);
    }


    /************************************************************************************************************************************************************************
     *  StoryContent相关方法
     ************************************************************************************************************************************************************************/
    @CachePut(value= DBCacheKey.STORY_CONTENT,key="#result.id")
    public StoryContentEntity insertStoryContentEntity(StoryContentEntity storyContentEntity) {
        storyContentMapper.insert(storyContentEntity);
        return storyContentEntity;
    }

    @CacheEvict(value= DBCacheKey.STORY_CONTENT,key="#p0.id")
    public void updateStoryContentEntity(StoryContentEntity storyContentEntity) {
        storyContentMapper.update(storyContentEntity);
    }

    @Cacheable(value= DBCacheKey.STORY_CONTENT,key="#p0")
    public StoryContentEntity selectStoryContentEntity(int storyId) {
        return storyContentMapper.select(storyId);
    }


    /************************************************************************************************************************************************************************
     *  StoryToGeography相关方法
     ************************************************************************************************************************************************************************/
    public void insertStoryToGeography(StoryToGeographyEntity entity){
        storyMapper.insertGeography(entity);
    }
    public List<String> selectGeographiesByTypeAndStoryId(StoryToGeographyEntity storyToGeographyEntity) {
        return storyMapper.selectGeographiesByTypeAndStoryId(storyToGeographyEntity);
    }

    public void deleteGeographiesByStoryId(int storyId) {
        storyMapper.deleteGeographiesByStoryId(storyId);
    }

    public List<GeographyEntity> selectGeographyListByStoryId(int storyId) {
        return storyMapper.selectGeographyListByStoryId(storyId);
    }

}
